Ext.define('Admin.view.main.factory.ColumnsFactory', {
    statics: {
        getNameColumn: function (name) {
            var module = Ext.StoreMgr.get('Models').getById(name);
            return [{
                text: '列表',
                dataIndex: module.get('name_field'),
                flex: 1
            }];
        },
        getColumns: function (name) {
            var module = Ext.StoreMgr.get('Models').getById(name),
                cols = module.get('columns'),
                columns = [],
                field, i;
            columns.push({
                xtype: 'rownumberer'
            });
            for (i = 0; i < cols.length; i++) {
                field = cols[i];
                columns.push({
                    xtype: this.getXType(field),
                    flex: !field.width ? 1 : void 0,
                    width: field.width ? field.width : void 0,
                    text: field.text,
                    dataIndex: field.name,
                    editor: field.editable ? this.getEditor(field) : void 0,
                    hidden: field.hidden,
                    align: this.getAlign(field),
                    filter: this.getFilter(field),
                    formatter: this.getFormatter(field)
                });
            }
            columns.push({
                xtype: 'actioncolumn',
                width: 30,
                sortable: false,
                menuDisabled: true,
                items: [{
                    icon: 'assets/images/delete.gif',
                    tooltip: '删除',
                    scope: this
                }]
            });
            return columns;
        },
        getXType: function (field) {
            switch (field.genre) {
                case 'boolean':
                    return 'checkcolumn';
                default:
                    return 'gridcolumn';
            }
        },
        getEditor: function (field) {
            var genre = field.genre.replace(')', '').split('(');
            switch (genre[0]) {
                case 'boolean':
                    return 'checkbox';
                case 'date':
                    return 'datefield';
                case 'int':
                case 'currency':
                case 'fileSize':
                case 'hex':
                case 'number':
                case 'percent':
                case 'plural':
                case 'round':
                case 'usMoney':
                    return 'numberfield';
                case 'enum':
                    return {
                        xtype: 'combo',
                        store: this.getEnums(genre[1])
                    };
                default:
                    return 'textfield';
            }
        },
        getFilter: function (field) {
            var genre = field.genre.replace(')', '').split('(');
            switch (genre[0]) {
                case 'boolean':
                    return 'boolean';
                case 'date':
                    return 'date';
                case 'int':
                case 'currency':
                case 'fileSize':
                case 'hex':
                case 'number':
                case 'percent':
                case 'plural':
                case 'round':
                case 'usMoney':
                    return 'number';
                case 'enum':
                    return {
                        type: 'list',
                        options: this.getEnums(genre[1])
                    };
                default:
                    return 'string';
            }
        },
        getFormatter: function (field) {
            var genre = field.genre.replace(')', '').split('(');
            switch (genre[0]) {
                case 'int':
                    return "number('0,000')";
                case 'string':
                case 'boolean':
                case 'enum':
                    return '';
                default:
                    if (genre.length >= 2) {
                        return genre[0] + "('" + genre[1] + "')";
                    } else {
                        return genre[0];
                    }
            }
        },
        getAlign: function (field) {
            var genre = field.genre.replace(')', '').split('(');
            switch (genre[0]) {
                case 'boolean':
                    return 'center';
                case 'int':
                case 'currency':
                case 'fileSize':
                case 'hex':
                case 'number':
                case 'percent':
                case 'plural':
                case 'round':
                case 'usMoney':
                    return 'right';
                default:
                    return 'left';
            }
        },
        getEnums: function (genres) {
            var gens = genres.split(','),
                enums = [],
                arr, gen, i;
            for (i = 0; i < gens.length; i++) {
                gen = gens[i];
                arr = gen.split(':');
                if (arr.length === 2) {
                    enums.push([parseInt(arr[0]), arr[1]]);
                } else {
                    enums.push(arr[0]);
                }
            }
            return enums;
        }
    }
});
